package org.dromara.wmscloud.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

import jakarta.servlet.ServletOutputStream;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.wmscloud.bean.LocaltionPdfData;
import org.dromara.wmscloud.utils.PDFService;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.wmscloud.domain.vo.BaseLocationVo;
import org.dromara.wmscloud.domain.bo.BaseLocationBo;
import org.dromara.wmscloud.service.IBaseLocationService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 货位
 *
 * @author hou
 * @date 2025-04-03
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/wmsCloud/location")
public class BaseLocationController extends BaseController {

    private final IBaseLocationService baseLocationService;
    private final PDFService pdfService;

    /**
     * 查询货位列表
     */
    @SaCheckPermission("base:location:list")
    @GetMapping("/list")
    public TableDataInfo<BaseLocationVo> list(BaseLocationBo bo, PageQuery pageQuery) {
        return baseLocationService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出货位列表
     */
    @SaCheckPermission("base:location:export")
    @Log(title = "货位", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(BaseLocationBo bo, HttpServletResponse response) {
        List<BaseLocationVo> list = baseLocationService.queryList(bo);
        ExcelUtil.exportExcel(list, "货位", BaseLocationVo.class, response);
    }

    /**
     * 获取货位详细信息
     *
     * @param locationId 主键
     */
    @SaCheckPermission("base:location:query")
    @GetMapping("/{locationId}")
    public R<BaseLocationVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long locationId) {
        return R.ok(baseLocationService.queryById(locationId));
    }

    /**
     * 新增货位
     */
    @SaCheckPermission("base:location:add")
    @Log(title = "货位", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody BaseLocationBo bo) {
        return toAjax(baseLocationService.insertByBo(bo));
    }

    /**
     * 修改货位
     */
    @SaCheckPermission("base:location:edit")
    @Log(title = "货位", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody BaseLocationBo bo) {
        return toAjax(baseLocationService.updateByBo(bo));
    }

    /**
     * 删除货位
     *
     * @param locationIds 主键串
     */
    @SaCheckPermission("base:location:remove")
    @Log(title = "货位", businessType = BusinessType.DELETE)
    @DeleteMapping("/{locationIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] locationIds) {
        return toAjax(baseLocationService.deleteWithValidByIds(List.of(locationIds), true));
    }

    /**
     * 打印货位
     *
     * @param locationId 主键串
     */
    @SaCheckPermission("base:location:print")
    @Log(title = "货位", businessType = BusinessType.OTHER)
    @GetMapping("printLocation/{locationId}")
    public void printLocation(HttpServletResponse response, @PathVariable Long locationId) throws Exception {
        response.setContentType("application/pdf");
        OutputStream out = response.getOutputStream();
        BaseLocationVo location = baseLocationService.queryById(locationId);
        if(location != null){
            LocaltionPdfData locationPdfData = new LocaltionPdfData();
            locationPdfData.setLocationCode(location.getLocationCode());
            locationPdfData.setLocationName(location.getLocationName());
            locationPdfData.setWarehouseCode(location.getWarehouseCode());
            InputStream in = new FileInputStream(new File(pdfService.printLocation(locationPdfData)));
            int len = 0;
            byte[] buffer = new byte[1024];
            while ((len = in.read(buffer)) > 0) {
                out.write(buffer, 0, len);
            }
            in.close();
        }
    }
}
